convert garmin_xt to Format class (#1230)
authortsteven4 <13596209+tsteven4@users.noreply.github.com>
Sat, 18 Nov 2023 23:28:47 +0000 (16:28 -0700)
committerGitHub <noreply@github.com>
Sat, 18 Nov 2023 23:28:47 +0000 (16:28 -0700)
CMakeLists.txt
garmin_xt.cc
garmin_xt.h [new file with mode: 0644]
vecs.cc

index 6be14d1508fe60af2b8d5d1280f9332356104c3d..38f2ff4cdea2f4ce74904c4a097c9143d993b844 100644 (file)
@@ -210,6 +210,7 @@ set(HEADERS
   gbfile.h
   gbser.h
   gbser_private.h
+  garmin_xt.h
   gdb.h
   geocache.h
   geojson.h
index 5e62af4dbf643eaa6c00dc629cdec6f63350da92..a0f4cc48e369f9a01a807c4c0d87eed628625f56 100644 (file)
 
 */
 
+#include "garmin_xt.h"
+
 #include <cstdint>   // for uint8_t, uint32_t, uint16_t, int32_t
 #include <cstdio>    // for SEEK_CUR, SEEK_SET
 #include <cstring>   // for strcmp, strcpy
 
 #include <QString>   // for QString
-#include <QVector>   // for QVector
 
-#include "defs.h"    //
-#include "gbfile.h"  // for gbfread, gbfgetuint16, gbfseek, gbfgetc, gbfgetu...
+#include "defs.h"
+#include "gbfile.h"  // for gbfread, gbfgetuint16, gbfseek, gbfgetc, gbfgetuint32, gbfclose, gbfeof, gbfopen
 
 
 #define MYNAME "Garmin_XT"
 #define DATABLOCKSIZE 1
 #define STRK_BLOCK_SIZE 97
 
-static int colors[] = {
-  0x000000, // Black
-  0x00008b, // DarkRed
-  0x006400, // DarkGreen
-  0x00d7ff, // Gold
-  0x8b0000, // DarkBlue
-  0x8b008b, // DarkMagenta
-  0x8b8b00, // DarkCyan
-  0xd3d3d3, // LightGray
-  0xa9a9a9, // DarkGray
-  0x0000ff, // Red
-  0x00ff00, // Green
-  0x00ffff, // Yellow
-  0xff0000, // Blue
-  0xff00ff, // Magenta
-  0xffff00, // Cyan
-  0xffffff // White
-};
-
-static gbfile* fin;
-static route_head* track;
-static char*   opt_xt_ftype = nullptr;
-static char*   opt_trk_header = nullptr;
-
-static
-QVector<arglist_t> format_garmin_xt_args = {
-  {"ftype", &opt_xt_ftype, "Garmin Mobile XT ([ATRK]/STRK)", "ATRK", ARGTYPE_STRING | ARGTYPE_REQUIRED, ARG_NOMINMAX, nullptr},
-  // TODO: SHIFT - can't test behaviour, do not have appropriate files
-  //{"trk_header_opt", &opt_trk_header, "Track name processing option ([0]-nrm/1-ign/2-sht)", "0", ARGTYPE_INT, ARG_NOMINMAX},
-  {"trk_header", &opt_trk_header, "Track name processing option ([0]-nrm/1-ign)", "0", ARGTYPE_INT, ARG_NOMINMAX, nullptr},
-};
-
 /*******************************************************************************
  * %%%        global callbacks called by gpsbabel main process              %%% *
  *******************************************************************************/
@@ -78,20 +47,20 @@ QVector<arglist_t> format_garmin_xt_args = {
 /*******************************************************************************
  * %%%        Reader callbacks                                              %%% *
  *******************************************************************************/
-static void
-format_garmin_xt_rd_init(const QString& fname)
+void
+GarminXTFormat::rd_init(const QString& fname)
 {
   fin = gbfopen(fname, "rb", MYNAME);
 }
 
-static void
-format_garmin_xt_rd_deinit()
+void
+GarminXTFormat::rd_deinit()
 {
   gbfclose(fin);
 }
 
-static uint16_t
-format_garmin_xt_rd_st_attrs(char* p_trk_name, uint8_t* p_track_color)
+uint16_t
+GarminXTFormat::format_garmin_xt_rd_st_attrs(char* p_trk_name, uint8_t* p_track_color)
 {
   int          method = 0;
   uint8_t      spam = 0;
@@ -156,8 +125,8 @@ format_garmin_xt_rd_st_attrs(char* p_trk_name, uint8_t* p_track_color)
 /*
  * Function to decrypt track block in saved read from saved tracks file
  */
-static void
-format_garmin_xt_decrypt_trk_blk(int Count, uint8_t TrackBlock[])
+void
+GarminXTFormat::format_garmin_xt_decrypt_trk_blk(int Count, uint8_t TrackBlock[])
 {
   int j = 12;
   while (j<(Count-1)) {
@@ -174,8 +143,8 @@ format_garmin_xt_decrypt_trk_blk(int Count, uint8_t TrackBlock[])
 /*
  * Function to Decompose track block of STRK_BLOCK_SIZE bytes
  */
-static void
-format_garmin_xt_decomp_trk_blk(uint8_t ii, const uint8_t TrackBlock[], double* Ele, double* Lat, double* Lon, uint32_t* Time)
+void
+GarminXTFormat::format_garmin_xt_decomp_trk_blk(uint8_t ii, const uint8_t TrackBlock[], double* Ele, double* Lat, double* Lon, uint32_t* Time)
 {
   //printf("%d %d %d %d %d %d\n", TrackBlock[0], TrackBlock[1], TrackBlock[2], TrackBlock[3], TrackBlock[4], TrackBlock[5]);
   uint16_t PrevEleW = TrackBlock[(ii - 1) * 12 + 1 ];
@@ -218,8 +187,8 @@ format_garmin_xt_decomp_trk_blk(uint8_t ii, const uint8_t TrackBlock[], double*
 /*
  * Decompose Last Waypoint Elevation
  */
-static void
-format_garmin_xt_decomp_last_ele(uint8_t ii, double* PrevEle, const uint8_t TrackBlock[])
+void
+GarminXTFormat::format_garmin_xt_decomp_last_ele(uint8_t ii, double* PrevEle, const uint8_t TrackBlock[])
 {
   uint16_t PrevEleW = TrackBlock[ii - 1];
   PrevEleW = PrevEleW << 8;
@@ -230,8 +199,8 @@ format_garmin_xt_decomp_last_ele(uint8_t ii, double* PrevEle, const uint8_t Trac
 /*
  * Main Function to process Saved tracks file
  */
-static void
-format_garmin_xt_proc_strk()
+void
+GarminXTFormat::format_garmin_xt_proc_strk()
 {
   int          Count = 0; // Used to obtain number of read bytes
   int TracksCompleted = 0; // Number of processed tracks
@@ -331,8 +300,8 @@ format_garmin_xt_proc_strk()
   }
 }
 
-static void
-format_garmin_xt_proc_atrk()
+void
+GarminXTFormat::format_garmin_xt_proc_atrk()
 {
   int          method = 0;
   unsigned char        buf[3];
@@ -393,8 +362,8 @@ format_garmin_xt_proc_atrk()
   }
 }
 
-static void
-format_garmin_xt_read()
+void
+GarminXTFormat::read()
 {
   // Saved Tracks file
   if (strcmp(opt_xt_ftype, "STRK") == 0) {
@@ -403,27 +372,3 @@ format_garmin_xt_read()
     format_garmin_xt_proc_atrk();
   }
 }
-
-/**************************************************************************/
-
-/* ascii is the expected character set */
-/* not fixed, can be changed through command line parameter */
-
-ff_vecs_t format_garmin_xt_vecs = {
-  ff_type_file,
-  {
-    ff_cap_none                                /* waypoints */,
-    ff_cap_read                                /* tracks */,
-    ff_cap_none                                /* routes */
-  },
-  format_garmin_xt_rd_init,
-  nullptr,
-  format_garmin_xt_rd_deinit,
-  nullptr,
-  format_garmin_xt_read,
-  nullptr,
-  nullptr,
-  &format_garmin_xt_args,
-  NULL_POS_OPS
-};
-/**************************************************************************/
diff --git a/garmin_xt.h b/garmin_xt.h
new file mode 100644 (file)
index 0000000..39c5b44
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+
+   Copyright (C) 2010  Eriks Zelenka, isindir@users.sourceforge.net
+   Copyright (C) 2009  jekaeff,
+   GMXT2GPX ( http://www.geocaching.hu/users.geo?id=9508 ; http://sites.google.com/site/jekaeff/eng-1 )
+   The original code written in Pascal and does not include specific License, however on the project
+   webpage it is said to be OpenSource/Libre software
+   Copyright (C) 2005  Robert Lipe, robertlipe+source@gpsbabel.org
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
+*/
+#ifndef GARMIN_XT_H_INCLUDED_
+#define GARMIN_XT_H_INCLUDED_
+
+#include <cstdint>   // for uint8_t, uint16_t, uint32_t
+
+#include <QString>   // for QString
+#include <QVector>   // for QVector
+
+#include "defs.h"
+#include "format.h"  // for Format
+#include "gbfile.h"  // for gbfile
+
+
+class GarminXTFormat : public Format
+{
+public:
+  QVector<arglist_t>* get_args() override
+  {
+    return &format_garmin_xt_args;
+  }
+
+  ff_type get_type() const override
+  {
+    return ff_type_file;
+  }
+
+  QVector<ff_cap> get_cap() const override
+  {
+    return {ff_cap_none, ff_cap_read, ff_cap_none};
+  }
+
+  void rd_init(const QString& fname) override;
+  void read() override;
+  void rd_deinit() override;
+
+private:
+  /* Constants */
+
+  static constexpr int colors[] = {
+    0x000000, // Black
+    0x00008b, // DarkRed
+    0x006400, // DarkGreen
+    0x00d7ff, // Gold
+    0x8b0000, // DarkBlue
+    0x8b008b, // DarkMagenta
+    0x8b8b00, // DarkCyan
+    0xd3d3d3, // LightGray
+    0xa9a9a9, // DarkGray
+    0x0000ff, // Red
+    0x00ff00, // Green
+    0x00ffff, // Yellow
+    0xff0000, // Blue
+    0xff00ff, // Magenta
+    0xffff00, // Cyan
+    0xffffff // White
+  };
+
+  /* Member Functions */
+
+  uint16_t format_garmin_xt_rd_st_attrs(char* p_trk_name, uint8_t* p_track_color);
+  static void format_garmin_xt_decrypt_trk_blk(int Count, uint8_t* TrackBlock);
+  static void format_garmin_xt_decomp_trk_blk(uint8_t ii, const uint8_t* TrackBlock, double* Ele, double* Lat, double* Lon, uint32_t* Time);
+  static void format_garmin_xt_decomp_last_ele(uint8_t ii, double* PrevEle, const uint8_t* TrackBlock);
+  void format_garmin_xt_proc_strk();
+  void format_garmin_xt_proc_atrk();
+
+  /* Data Members */
+
+  gbfile* fin{};
+  route_head* track{};
+  char*        opt_xt_ftype = nullptr;
+  char*        opt_trk_header = nullptr;
+
+  QVector<arglist_t> format_garmin_xt_args = {
+    {"ftype", &opt_xt_ftype, "Garmin Mobile XT ([ATRK]/STRK)", "ATRK", ARGTYPE_STRING | ARGTYPE_REQUIRED, ARG_NOMINMAX, nullptr},
+    // TODO: SHIFT - can't test behaviour, do not have appropriate files
+    //{"trk_header_opt", &opt_trk_header, "Track name processing option ([0]-nrm/1-ign/2-sht)", "0", ARGTYPE_INT, ARG_NOMINMAX},
+    {"trk_header", &opt_trk_header, "Track name processing option ([0]-nrm/1-ign)", "0", ARGTYPE_INT, ARG_NOMINMAX, nullptr},
+  };
+};
+#endif // GARMIN_XT_H_INCLUDED_
diff --git a/vecs.cc b/vecs.cc
index 4807ad9496723e4a72477e9e11a1e30d5a5809cd..a963531f856809363cc133cc4c743bd09861f115 100644 (file)
--- a/vecs.cc
+++ b/vecs.cc
@@ -46,6 +46,7 @@
 #include "garmin.h"            // for GarminFormat
 #include "garmin_fit.h"        // for GarminFitFormat
 #include "garmin_gpi.h"        // for GarminGPIFormat
+#include "garmin_xt.h"         // for GarminXTFormat
 #include "gbversion.h"         // for WEB_DOC_DIR
 #include "gdb.h"               // for GdbFormat
 #include "geojson.h"           // for GeoJsonFormat
@@ -93,7 +94,6 @@ extern ff_vecs_t mtk_m241_fvecs;
 extern ff_vecs_t garmin_txt_vecs;
 #endif // CSVFMTS_ENABLED
 extern ff_vecs_t ggv_log_vecs;
-extern ff_vecs_t format_garmin_xt_vecs;
 #endif // MAXIMAL_ENABLED
 
 #define MYNAME "vecs"
@@ -155,7 +155,7 @@ struct Vecs::Impl {
   SkytraqfileFormat skytraq_ffmt;
   MinihomerFormat miniHomer_fmt;
   SubripFormat subrip_fmt;
-  LegacyFormat format_garmin_xt_fmt {format_garmin_xt_vecs};
+  GarminXTFormat format_garmin_xt_fmt;
   GarminFitFormat format_fit_fmt;
   GeoJsonFormat geojson_fmt;
   GlobalsatSportFormat globalsat_sport_fmt;